import { Module } from 'vuex'
import { UserState, AllState } from './types'

const userModule: Module<UserState, AllState> = {
  namespaced: true,
  state: () => ({
    name: 'John Doe',
    age: 30
  }),
  mutations: {
    setName(state: UserState, name: string) {
      state.name = name
    },
    setAge(state: UserState, age: number) {
      state.age = age
    }
  },
  actions: {
    updateUser({ commit }, payload: { name: string; age: number }) {
      commit('setName', payload.name)
      commit('setAge', payload.age)
    }
  },
  getters: {
    userInfo: (state: UserState) => `${state.name}, ${state.age} years old`
  }
}

export default userModule